package com.lanou3g.farmdoctor.activity;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;

import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.application.FDApplication;
import com.lanou3g.farmdoctor.fragment.FindFragment;
import com.lanou3g.farmdoctor.fragment.ForRichExpFragment;
import com.lanou3g.farmdoctor.fragment.OwnFragment;
import com.lanou3g.farmdoctor.fragment.QandAFragment;

public class MainActivity extends AppCompatActivity {

    private TabHost tabHost;

    public TabHost getTabHost() {
        return tabHost;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        FDApplication.setMainActivity(this);
    }

    /**
     * 加载组件
     */
    private void initView() {
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        // 设置标签
        TabHost.TabSpec tab0 = tabHost.newTabSpec("tab0");
        View view0 = getLayoutInflater().inflate(R.layout.tabs_q_and_a, null);
        tab0.setIndicator(view0);
        tab0.setContent(R.id.main_q_and_a_view);
        tabHost.addTab(tab0);

        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
        View view1 = getLayoutInflater().inflate(R.layout.tabs_for_rich, null);
        tab1.setIndicator(view1);
        tab1.setContent(R.id.main_for_rich_view);
        tabHost.addTab(tab1);

        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
        View view2 = getLayoutInflater().inflate(R.layout.tabs_fast_ask, null);
        tab2.setIndicator(view2);
        tab2.setContent(R.id.main_fast_ask_view);
        tabHost.addTab(tab2);

        TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3");
        View view3 = getLayoutInflater().inflate(R.layout.tabs_find, null);
        tab3.setIndicator(view3);
        tab3.setContent(R.id.main_find_view);
        tabHost.addTab(tab3);

        TabHost.TabSpec tab4 = tabHost.newTabSpec("tab4");
        View view4 = getLayoutInflater().inflate(R.layout.tabs_own, null);
        tab4.setIndicator(view4);
        tab4.setContent(R.id.main_own_view);
        tabHost.addTab(tab4);

        tabHost.setCurrentTab(0);

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                switch (tabId) {
                    case "tab2":
                        Intent intent = new Intent(MainActivity.this, QuickQuestionAty.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.quick_qusetion_activity_in_anim, R.anim.out_anim);
                        break;
                }
            }
        });
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_q_and_a_view, new QandAFragment(), "QandAFragment");
        transaction.replace(R.id.main_for_rich_view, new ForRichExpFragment());
        transaction.replace(R.id.main_find_view, new FindFragment());
        transaction.replace(R.id.main_own_view, new OwnFragment());
        transaction.commit();
    }
}
